<?php
/*. require_module 'standard'; .*/
/**
 * Methods for correctly creating and handling html
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 * @link       http://my-trac.assembla.com/amemorytree/ticket/163
 */
class Html
{
    /**
     * Convert escaped values by removing slashes
     *
     * The text is stripped of any slashes, due to database protection
     * methods used by $_GET, $_POST and database values.
     *
     * @param mixed $value The text to be made safe
     * @return string      The text after it has been made safe for display
     */
    public function text($value)
    {
        if (get_magic_quotes_gpc() === 1) {
            $value = stripslashes((string)$value);
        }
        return htmlentities((string)$value, ENT_QUOTES, 'UTF-8');
    }
    /**
     * Create an option field from a row procided by the database
     * 
     * An example of the returned string is:
     * <option value="64">New Zealand</option>
     * 
     * @param array $row     The row, in which the value is the first field and the text is the second
     * @param array $options (Optional) isSelected and padding options
     *                       isSelected: Sets the option as selected by default
     *                       padding   : The amount of spacing to add before the option 
     * @return string        The completed option field
     */
    public function option($row, $options=array())
    {
        $values = array_values($row);
        $value = $values[0];
        $text = $values[1];
        $isSelected = FALSE;
        $padding = 0;
        // :TODO: helper for arrayKeyOrDefault($array, $key, $default)
        if (count($options) > 0) {
            if (array_key_exists('selected', $options)) {
                if ($options['selected'] === $value) {
                    $isSelected = true;
                }
            }
            if (array_key_exists('padding', $options)) {
                $padding = $options['padding'];
            }
        }
        $selected = '';
        if($isSelected === TRUE) {
            $selected = ' selected="selected"';
        }
        $option = '<option'.$selected.' value="'.$value.'">'.$text.'</option>';
        return str_pad($option, $padding);
    }
}
?>
